<?php

require_once ROOT_DIR.'models/Message.php';

/**
 * Tries to modify the message stored under the given id and echos the resulting view
 * @param int $id
 * @param string $title
 * @param string $text
 * @param id $messageID
 * @param id $todoID
 * @param id $listID
 * @param id $userID
 */
function editMsg($id, $title, $text, $messageID, $todoID, $listID, $userID){
	$msg = Message::getMessage($id);
	if(!is_null($msg)){
		if($msg->getAuthorID() == $_SESSION['USER_ID'] || $_SESSION['USER_NAME'] == 'admin'){
			if(!is_null($title)){
				$msg->setTitle($title);
			}
			
			if(!is_null($text)){
				$msg->setText($text);
			}
			
			if(!is_null($messageID)){
				$msg->setMessageID($messageID);
			}
			
			if(!is_null($todoID)){
				$msg->setTodoID($todoID);
			}
			
			if(!is_null($listID)){
				$msg->setListID($listID);
			}
			
			if(!is_null($userID)){
				$msg->setUserID($userID);
			}
			
			if($msg->saveToDB()){
				$res = new ResultJSON(false, session_id());
				$res->addItem('mID', $msg->getID());
				echo $res->renderView();
			} else {
				$res = new ResultJSON(true, session_id(), ERROR_ACTION_FAILED);
				echo $res->renderView();
			}
		} else {
			$res = new ResultJSON(true, session_id(), ERROR_NOT_ALLOWED);
			echo $res->renderView();
		}
	} else {
		$res = new ResultJSON(true, session_id(), ERROR_MSG_DOES_NOT_EXIST);
		echo $res->renderView();
	}
}

?>